<template>
  <!-- 搜索工作栏 -->
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="单位名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入部门名称"
          clearable
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"> <Icon icon="ep:search" class="mr-5px" /> 搜索 </el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['system:unitManage:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 添加
        </el-button>
        <el-button
          type="primary"
          plain
          @click="deleteIds"
          v-hasPermi="['system:unitManage:deleteIds']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 批量删除
        </el-button>
        <!-- <el-button type="danger" plain @click="toggleExpandAll">
          <Icon icon="ep:sort" class="mr-5px" /> 展开/折叠
        </el-button> -->
      </el-form-item>
    </el-form>
  </ContentWrap>
  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align="center" key="id" prop="id" />
      <el-table-column prop="name" label="单位名称" width="260" />
      <el-table-column prop="address" label="单位地址" width="200" />
      <el-table-column prop="unitType" label="单位类型" width="200" :formatter="UnitTypeData" />
      <el-table-column label="操作" align="center" class-name="fixed-width">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['system:unitManage:update']"
          >
            修改
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['system:unitManage:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
  <!-- 表单弹窗：添加/修改 -->
  <UnitForm ref="formRef" @success="getList" />
</template>
<script lang="ts" setup>
// import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
// import { dateFormatter } from '@/utils/formatTime'
import * as UnitApi from '@/api/system/unit'
import UnitForm from './UnitForm.vue'
import { UnitVO } from '@/api/system/unit'
import { DictDataType, getIntDictOptions } from '@/utils/dict'
// import { Message } from '@/layout/components/Message';
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数量
const multipleSelection = ref<UnitVO[]>([]) // 批量删除要保存的数组
const handleSelectionChange = (val: UnitVO[]) => {
  multipleSelection.value = val
}
/**定义一个回显单位类型的数组 */
let dictUnit = ref<DictDataType[]>([])
/**获取单位类型数组 */
const getDictUnit = () => {
  dictUnit.value = getIntDictOptions(DICT_TYPE.UNIT_TYPE)
}
/**在列表中显示对应的label值 */
const UnitTypeData = (row) => {
  if (dictUnit.value) {
    for (let i = 0; i < dictUnit.value.length; i++) {
      if (dictUnit.value[i].value == row.unitType) {
        return dictUnit.value[i].label
      }
    }
  }
}
/**初始化查询参数 */
const queryParams = reactive({
  name: '',
  pageNo: 1,
  pageSize: 10
})
const deleteIds = async () => {
  // 批量删除 获取id数组
  if (multipleSelection.value.length == 0) {
    message.warning('请选择要删除的选项')
  } else {
    try {
      const ids = multipleSelection.value.map((multiple) => multiple.id)
      //确认删除
      await message.delConfirm()
      await UnitApi.deleteUnitList(ids)
      message.success(t('common.delSuccess'))
      // 刷新列表
      await getList()
    } catch {}
  }
}
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await UnitApi.getUnitPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}
/**添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}
/**删除操作 */
const handleDelete = async (id: number) => {
  try {
    // 确认删除?
    await message.delConfirm()
    // 删除
    UnitApi.deleteUnit(id)
    await message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}
/** 搜索按钮操作 */
const handleQuery = () => {
  getList()
}
/** 初始化 */
onMounted(() => {
  getList()
  getDictUnit()
})
</script>
